package edu.ues21.tcs.ecommerce2011.mvc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.ues21.tcs.ecommerce2011.mvc.dao.ClienteDAO;
import edu.ues21.tcs.ecommerce2011.mvc.model.Cliente;

/**
 * 
 * @author fmantaras.
 */
public class AddClienteController extends HttpServlet {

	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
			"/BeanLocations.xml");

	ClienteDAO cDao = (ClienteDAO) appContext.getBean("clienteDAO");

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
	}

	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Integer cuit = Integer.parseInt(request.getParameter("cuit").trim());
		String rsocial = request.getParameter("razonSocial");
		Integer telefono = Integer.parseInt(request.getParameter("telefono").trim());
		String domicilio = request.getParameter("domicilio");
		System.out.println("++++++++++++++++++++++++++++ " + cuit);
		if(cuit != null & rsocial != null){
			Cliente c = new Cliente();
			c.setCuit(cuit);
			c.setRazonSocial(rsocial);
			c.setDomicilio(domicilio);
			c.setTelefono(telefono);
			cDao.save(c);
		}
		response.sendRedirect("CRUDClientes.do");
			
	}

	
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>


}